package day11.sax;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

/**
 * @author oldliu
 * @since 1.0
 */
public class Java76Handler extends DefaultHandler {
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        System.out.println("节点的值："+new String(ch,start,length));
    }
    @Override//文档结束
    public void endDocument() throws SAXException {
        System.out.println("文档结果了");
        super.endDocument();
    }

    @Override//节点结束了
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        System.out.println(localName+","+qName+"元素结束了");
//
//		if(qName.equals("book"))
//		{
//			//list.add(b);
//		}
    }

    @Override//文档开始
    public void startDocument() throws SAXException {
        System.out.println("文档开始了");
        super.startDocument();
    }
    //Book b=null;
    @Override//元素开始
    public void startElement(String uri, String localName, String qName,
                             Attributes attributes) throws SAXException {

        System.out.println("元素开始了");
        System.out.println(localName+","+qName);

        for(int i=0;attributes!=null && i<attributes.getLength();i++)
        {
            System.out.println(attributes.getQName(i)+","+attributes.getValue(i));

        }
    }

    public static void main(String[] args) throws Exception {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser = factory.newSAXParser();
        parser.parse("课工场76.xml", new Java76Handler());

    }
}
